﻿Public Class frmSanPham

    Public Event OnUpdate As EventHandler(Of EventArgs)
    Protected Sub SetUpdate(ban As SanPham)
        RaiseEvent OnUpdate(ban, New EventArgs())
    End Sub
    ReadOnly _db As QlqaContext = New QlqaContext()
    Private Sub lvDs_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvDs.SelectedIndexChanged
        If (lvDs.SelectedItems.Count) Then
            txtTen.Text = lvDs.SelectedItems(0).SubItems(1).Text
            nudGia.Value = lvDs.SelectedItems(0).SubItems(3).Text
            cboDanhMuc.Text = lvDs.SelectedItems(0).SubItems(2).Text
        End If
    End Sub

    Private Sub lvDs_MouseDoubleClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles lvDs.MouseDoubleClick
        txtTen.Text = lvDs.SelectedItems(0).SubItems(1).Text
    End Sub

    Private Sub CapNhatSoThuTu()
        For i As Integer = 1 To lvDs.Items.Count
            lvDs.Items(i - 1).Text = i.ToString()
        Next
    End Sub

    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnThem.Click

        Dim obj As SanPham = New SanPham()

        obj.Ten = txtTen.Text
        obj.Gia = nudGia.Value

        obj.DanhMuc = _db.DanhMucs.FirstOrDefault(Function(x) x.Ten = cboDanhMuc.Text)
        _db.SanPhams.Add(obj)

        _db.SaveChanges()
        LoadDs()
        SetUpdate(obj)
    End Sub

    Private Sub frmSanPham_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        LoadDs()
        LoadInput()
    End Sub

    Sub LoadInput()
        cboDanhMuc.DataSource = _db.DanhMucs.ToArray()
        cboDanhMuc.DisplayMember = "Ten"
        cboDanhMuc.ValueMember = "Id"
    End Sub

    Sub LoadDs()
        lvDs.Items.Clear()

        For Each o In _db.SanPhams.Select(Function(x) New With {x.Id, x.Gia, x.Ten, x.DanhMuc}).ToList()
            Dim li = New ListViewItem()
            li.Tag = o
            li.SubItems.Add(o.Ten)
            li.SubItems.Add(o.DanhMuc.Ten)
            li.SubItems.Add(o.Gia)
            lvDs.Items.Add(li)
        Next
        CapNhatSoThuTu()
    End Sub

    Private Sub btnXoa_Click(sender As System.Object, e As System.EventArgs) Handles btnXoa.Click
        If lvDs.SelectedItems.Count = 0 Then Return

        Dim id As Int32 = Convert.ToInt32(lvDs.SelectedItems(0).Tag.Id)

        Dim obj As SanPham = _db.SanPhams.FirstOrDefault(Function(x) x.Id = id)
        If Not IsNothing(obj) Then
            _db.SanPhams.Remove(obj)
            _db.SaveChanges()
            ' và xóa dòng được trên thể hiện ListView
            lvDs.SelectedItems(0).Remove()
            ' Cập nhật lại số thứ tự
            CapNhatSoThuTu()
        End If
        SetUpdate(obj)

    End Sub

    Private Sub btnCapNhat_Click(sender As System.Object, e As System.EventArgs) Handles btnCapNhat.Click
        If lvDs.SelectedItems.Count = 0 Then Return
        ' Lấy số thứ tự của dòng đang được chọn
        Dim selectedIndex As Integer = CType(lvDs.SelectedItems(0).Text, Integer) - 1

        Dim id As Int32 = Convert.ToInt32(lvDs.SelectedItems(0).Tag.Id)

        Dim obj As SanPham = _db.SanPhams.FirstOrDefault(Function(x) x.Id = id)

        If Not IsNothing(obj) Then

            obj.Ten = txtTen.Text
            obj.Gia = nudGia.Value
            obj.DanhMuc = _db.DanhMucs.FirstOrDefault(Function(x) x.Ten = cboDanhMuc.Text)

            _db.SaveChanges()

            ' Cập nhật lại trên thể hiện ListView
            lvDs.Items(selectedIndex).SubItems(1).Text = obj.Ten
            lvDs.Items(selectedIndex).SubItems(2).Text = obj.DanhMuc.Ten
            lvDs.Items(selectedIndex).SubItems(3).Text = obj.Gia

            ' Cập nhật lại số thứ tự
            CapNhatSoThuTu()
        End If

        SetUpdate(obj)

    End Sub


    Private Sub btnSoanLai_Click(sender As System.Object, e As System.EventArgs) Handles btnSoanLai.Click
        txtTen.Text = String.Empty
        nudGia.Value = 0
    End Sub
End Class